const initialState = {
  commentList: [] as CommentType[],
  loading: false,
  // 1. reducer中设置originComment，设置初始值为{}
  originComment: {} as CommentType,
  replayComments: {
    end_id: '',
    last_id: '',
    results: [] as ReplyCommentType[],
    total_count: 0,
  },
};
type actionType =
  | { type: 'comment/saveList'; payload: CommentType[] }
  | { type: 'comment/saveLoading'; payload: boolean }
  | { type: 'comment/setLoading'; payload: boolean }
  | { type: 'comment/saveOrigiComment'; payload: CommentType }
  | { type: 'comment/clearOrigiComment' }
  | {
      type: 'comment/saveReplyComment';
      payload: typeof initialState.replayComments;
    }
  | { type: 'comment/updateCommentById'; payload: string }
  | { type: 'comment/updateReplyCommentById'; payload: string }
  | { type: 'comment/updateOriginComment' };

// eslint-disable-next-line import/no-anonymous-default-export
export default (
  state = initialState,
  action: actionType
): typeof initialState => {
  switch (action.type) {
    case 'comment/saveList':
      return {
        ...state,
        commentList: [...action.payload],
      };
    case 'comment/saveLoading':
      return {
        ...state,
        loading: action.payload,
      };
    // 3. switch逻辑分支
    case 'comment/saveOrigiComment':
      return {
        ...state,
        originComment: action.payload,
      };
    case 'comment/clearOrigiComment':
      return {
        ...state,
        originComment: {} as CommentType,
      };
    case 'comment/saveReplyComment':
      return {
        ...state,
        replayComments: { ...action.payload },
      };
    case 'comment/updateCommentById':
      return {
        ...state,
        // 覆盖旧的list
        commentList: updateListById(state.commentList, action.payload),
      };
    case 'comment/updateReplyCommentById':
      return {
        ...state,
        replayComments: {
          ...state.replayComments,
          // 覆盖旧的list
          results: updateListById(state.replayComments.results, action.payload),
        },
      };
    // 2. 实现更新原始数据的逻辑
    case 'comment/updateOriginComment':
      return {
        ...state,
        originComment: {
          ...state.originComment,
          is_liking: !state.originComment.is_liking,
          like_count: state.originComment.is_liking
            ? state.originComment.like_count - 1
            : state.originComment.like_count + 1,
        },
      };
    default:
      return state;
  }
};

export type CommentType = {
  com_id: string;
  content: string;
  reply_count: number;
  pubdate: string;
  is_followed: boolean;
  is_liking: boolean;
  like_count: number;
  aut_id: string;
  aut_name: string;
  aut_photo: string;
};

export type ReplyCommentType = {
  com_id: string;
  content: string;
  reply_count: number;
  pubdate: string;
  is_followed: boolean;
  is_liking: boolean;
  like_count: number;
  aut_id: string;
  aut_name: string;
  aut_photo: string;
};

function updateListById(list: CommentType[], id: string): CommentType[] {
  // 拿到要更新的index值
  const updateIndex = list.findIndex((item) => item.com_id === id);
  let updateItem = { ...list[updateIndex] };
  if (updateItem.is_liking) {
    updateItem.like_count--;
  } else {
    updateItem.like_count++;
  }
  updateItem.is_liking = !updateItem.is_liking;

  // 生成一个新的list
  let newList = list.slice();
  newList[updateIndex] = updateItem;
  return newList;
}
